package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.FamilyBaseInfoAddRequest;
import com.frogs.ucenter.v1.request.FamilyBaseInfoDetailRequest;
import com.frogs.ucenter.v1.request.FamilyBaseInfoUpdateRequest;
import com.frogs.ucenter.v1.request.FamilyDataStatRequest;
import com.frogs.ucenter.v1.request.FamilyListRequest;
import com.frogs.ucenter.v1.request.FamilyRankingListRequest;
import com.frogs.ucenter.v1.service.FamilyBaseInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION+"familyBaseInfoController")
@RequestMapping("/"+ApiConstants.VERSION)
public class FamilyBaseInfoController extends BaseController3 {
	
	@Resource(name=ApiConstants.VERSION + "familyBaseInfoService")
	private FamilyBaseInfoService familyBaseInfoService;

    private static final String ROOT = "/family";

    //新建
    private static final String FAMILY_BASEINFO_ADD = ROOT + "/baseinfo" + ADD;
    //修改
    private static final String FAMILY_BASEINFO_UPDATE = ROOT + "/baseinfo" + UPDATE;
    //详情
    private static final String FAMILY_BASEINFO_DETAIL = ROOT + "/baseinfo" + DETAIL;
    //家庭圈列表
    private static final String FAMILY_LIST = ROOT + LIST;
    //排名
    private static final String FAMILY_RANKING_LIST = ROOT + "/ranking" + LIST;
    //统计成员数、繁荣度
    private static final String FAMILY_DATA_STAT = ROOT + "/data" + "/stat";

	/**
	 * 家庭信息新增（创建用户时，家庭圈同时也建立，暂时不会单独调用到家庭圈的建立）
	 * @param request
	 * @return
	 */
	@RequestMapping(FAMILY_BASEINFO_ADD)
	public Response familyBaseInfoAdd(HttpServletRequest request) {
		FamilyBaseInfoAddRequest familyBaseInfoAddRequest = new FamilyBaseInfoAddRequest(); 
		Response response;
		try {
			familyBaseInfoAddRequest.parse(request);
			response = familyBaseInfoService.addFamilyBaseInfo(familyBaseInfoAddRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FAMILY_BASEINFO_UPDATE)
	public Response familyBaseInfoUpdate(HttpServletRequest request) {
		FamilyBaseInfoUpdateRequest familyBaseInfoUpdateRequest = new FamilyBaseInfoUpdateRequest(); 
		Response response;
		try {
			familyBaseInfoUpdateRequest.parse(request);
			response = familyBaseInfoService.updateFamilyBaseInfo(familyBaseInfoUpdateRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FAMILY_BASEINFO_DETAIL)
	public Response familyBaseInfoDetail(HttpServletRequest request) {
		FamilyBaseInfoDetailRequest familyBaseInfoDetailRequest = new FamilyBaseInfoDetailRequest(); 
		Response response;
		try {
			familyBaseInfoDetailRequest.parse(request);
			response = familyBaseInfoService.getFamilyBaseInfo(familyBaseInfoDetailRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FAMILY_RANKING_LIST)
	public Response familyRankingList(HttpServletRequest request) {
		FamilyRankingListRequest familyRankingListRequest = new FamilyRankingListRequest();
		Response response;
		try {
			familyRankingListRequest.parse(request);
			response = familyBaseInfoService.getRankingList(familyRankingListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FAMILY_LIST)
	public Response familyList(HttpServletRequest request) {
		FamilyListRequest familyListRequest = new FamilyListRequest(); 
		Response response;
		try {
			familyListRequest.parse(request);
			response = familyBaseInfoService.getFamilyList(familyListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping(FAMILY_DATA_STAT)
	public Response familyDataStatistics(HttpServletRequest request) {
		FamilyDataStatRequest familyDataStatRequest = new FamilyDataStatRequest(); 
		Response response;
		try {
			familyDataStatRequest.parse(request);
			response = familyBaseInfoService.statistics(familyDataStatRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
